home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 099 (1989-05-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 099 (1989-05-15)(Ossowski, Stefan)(DE)(PD).adf / PCQ / Examples / Snow.p < prev    next >
Text File  |  1989-03-31  |  5KB  |  195 lines

  1. Program Snowflake;
  2.  
  3. { This program draws a fractal snowflake pattern.  I think I got it out
  4. of some magazine years ago.  It was written, as I remember it, for the
  5. PC in BASIC, which I converted to AmigaBASIC.  I have long since
  6. forgotten the details of how it worked, so I could not give the
  7. variables meaningful names.  To the original author, by the way, goes
  8. the credit for those names.  Invoke the program with the line "Snow
  9. <level>", where <level> is a digit between 1 and 6.  In order to get a
  10. feel for what's going on, try running the levels in order.  Level 6
  11. takes a long time, and frankly doesn't look as good as level 5.  }
  12.  
  13. {$I "Include/Intuition.i" for the windows }
  14. {$I "Include/Math.i" for the basic floating point stuff }
  15. {$I "Include/Mathtrans.i" for sin, cos, and sqrt}
  16. {$I "Include/Graphics.i" for move() and draw() }
  17. {$I "Include/Exec.i" just for OpenLibrary and CloseLibrary }
  18. {$I "Include/Ports.i" for GetMsg and WaitPort }
  19.  
  20. var
  21.     dx : array [0..11] of real;
  22.     dy : array [0..11] of real;
  23.     sd : array [0..6] of integer;
  24.     rd : array [0..6] of integer;
  25.     sn : array [0..6] of integer;
  26.     ln : array [0..6] of real;
  27.     a  : real;
  28.     nc : integer;
  29.     x, y, t : real;
  30.     w  : WindowPtr;
  31.     rp : RastPortPtr;
  32.     n  : integer;
  33.     d, ns, i, j : integer;
  34.     l : real;
  35.     rcon133, rcon05 : real;
  36.     m : MessagePtr;
  37.  
  38. Procedure usage;
  39. begin
  40.     writeln('Usage: Snow <level>');
  41.     writeln('       where <level> is between 1 and 6');
  42.     exit(20);
  43. end;
  44.  
  45. Function readcycles(): integer;
  46. var
  47.     index : integer;
  48.     cycles : integer;
  49. begin
  50.     index := 1;
  51.     while ((commandline[index] = ' ') or (commandline[index] = chr(9))) and
  52.     (index < 128) do
  53.     index := index + 1;
  54.     if index >= 128 then
  55.     usage;
  56.     cycles := ord(commandline[index]) - ord('0');
  57.     if (cycles > 6) or (cycles < 1) then
  58.     usage;
  59.     readcycles := cycles;
  60. end;
  61.  
  62. Function OpenTheWindow() : Boolean;
  63. var
  64.     nw : NewWindowPtr;
  65. begin
  66.     new(nw);
  67.  
  68.     nw^.LeftEdge := 0;
  69.     nw^.TopEdge := 0;
  70.     nw^.Width := 640;
  71.     nw^.Height := 200;
  72.  
  73.     nw^.DetailPen := -1;
  74.     nw^.BlockPen  := -1;
  75.     nw^.IDCMPFlags := CLOSEWINDOW_f;
  76.     nw^.Flags := WINDOWDEPTH_f + WINDOWCLOSE_f + SMART_REFRESH_f + ACTIVATE_f;
  77.     nw^.FirstGadget := nil;
  78.     nw^.CheckMark := nil;
  79.     nw^.Title := "Fractal Snowflake";
  80.     nw^.Screen := nil;
  81.     nw^.BitMap := nil;
  82.     nw^.MinWidth := 50;
  83.     nw^.MaxWidth := -1;
  84.     nw^.MinHeight := 20;
  85.     nw^.MaxHeight := -1;
  86.     nw^.WType := WBENCHSCREEN_f;
  87.  
  88.     w := OpenWindow(nw);
  89.     dispose(nw);
  90.     OpenTheWindow := w <> nil;
  91. end;
  92.  
  93. procedure initarrays;
  94. begin
  95.     sd[0] := 0;
  96.     rd[0] := 0;
  97.     sd[1] := 1;
  98.     rd[1] := 0;
  99.     sd[2] := 1;
  100.     rd[2] := 7;
  101.     sd[3] := 0;
  102.     rd[3] := 10;
  103.     sd[4] := 0;
  104.     rd[4] := 0;
  105.     sd[5] := 0;
  106.     rd[5] := 2;
  107.     sd[6] := 1;
  108.     rd[6] := 2;
  109.  
  110.     for n := 0 to 6 do
  111.     ln[n] := spdiv(spfloat(1), spfloat(3));
  112.     ln[2] := spsqrt(ln[1]);
  113.     a := spfloat(0);
  114.     for n := 6 to 11 do begin
  115.     dy[n] := spsincos(dx[n], a);
  116.         a := spadd(a, spdiv(spfloat(52359), spfloat(100000)));
  117.     end;
  118.     for n := 0 to 5 do begin
  119.     dx[n] := spneg(dx[n + 6]);
  120.     dy[n] := spneg(dy[n + 6]);
  121.     end;
  122.     x := spfloat(534);
  123.     y := spfloat(151);
  124.     t := spfloat(324);
  125.     rcon133 := spdiv(spfloat(4), spfloat(3));
  126.     rcon05  := spdiv(spfloat(1), spfloat(2));
  127. end;
  128.  
  129. begin
  130.     nc := readcycles();
  131.     if not OpenMathTrans() then begin
  132.     writeln('Could not open MathTrans.library.');
  133.     exit(20);
  134.     end;
  135.     initarrays;
  136.  
  137.     GfxBase := OpenLibrary("graphics.library", 0);
  138.     if GfxBase = nil then begin
  139.     writeln('Could not open Graphics.library');
  140.     FlushMathTrans;
  141.     exit(20);
  142.     end;
  143.  
  144.     if OpenTheWindow() then begin
  145.     rp := w^.RPort;
  146.  
  147.     for n := 0 to nc do
  148.         sn[n] := 0;
  149.  
  150.     Move(rp, spfix(x), spfix(y));
  151.  
  152.     repeat
  153.         d := 0;
  154.         l := t;
  155.         ns := 0;
  156.  
  157.         for n := 1 to nc do begin
  158.         i := sn[n];
  159.         l := spmul(l, ln[i]);
  160.         j := sn[n - 1];
  161.         ns := ns + sd[j];
  162.         if odd(ns) then
  163.             d := (d + 12 - rd[i]) mod 12
  164.         else
  165.             d := (d + rd[i]) mod 12;
  166.         end;
  167.  
  168.         { x := x + 1.33 * l * dx[d]; }
  169.         x := spadd(x, spmul(spmul(rcon133, l), dx[d]));
  170.  
  171.         { y := y - 0.5 * l * dy[d]; }
  172.         y := spsub(y, spmul(spmul(l, dy[d]), rcon05));
  173.  
  174.         Draw(rp, spfix(x), spfix(y));
  175.         sn[nc] := sn[nc] + 1;
  176.         n := nc;
  177.         while (n >= 1) and (sn[n] = 7) do begin
  178.         sn[n] := 0;
  179.         sn[n - 1] := sn[n - 1] + 1;
  180.         n := n - 1;
  181.         end;
  182.     until sn[0] <> 0;
  183.     m := WaitPort(w^.UserPort);
  184.     forbid;
  185.     repeat
  186.         m := GetMsg(w^.UserPort);
  187.     until m = nil;
  188.     permit;
  189.     CloseWindow(w);
  190.     end else
  191.     writeln('Could not open the window');
  192.     CloseLibrary(GfxBase);
  193.     FlushMathTrans;
  194. end.
  195.